/*
 * power.cpp
 *
 *  Created on: Aug 7, 2012
 *      Author: jh238d
 */
#include <iostream>
using namespace std;

double power2(int x, int y){
	try{
		if(x == 0)
			throw "wrong input!";
	}catch(char const* str){
		cout << str << endl;
		return NULL;
	}
	double result = 1;
	bool minus = false;
	if (y < 0){
		y = 0-y;
		minus = true;
	}
	for (int j = 0; j < y; j++) {
		result *= x;
	}
	if(minus)
		result = 1/result;
	return result;
}

double power(int x, int y){
	double result = 1;
	bool minus = false;
	if (y < 0){
		y = 0-y;
		minus = true;
	}
	while(y!=0){
		if((y&1)!=0)
			result *=x;
		x*=x;
		y>>=1;
	}
	if(minus)
		result = 1/result;
	return result;
}

/*int main(){
	cout<< power(3,4)<< endl;
	cout<< power(3,0)<< endl;
	cout<< power(-3, 4) << endl;
	cout<<power(-3,3) << endl;
	cout<<power(3,-1)<<endl;
	cout << power(0, 1) << endl;
	//To Do: How about x, y that will cause overflow???
	return 0;
}*/


